<template>
  <div class="rating-select">
    <div class="rating-type border-bottom">
      <div @click="selectRatingType(2)" :class="{'active':currentSelectType === 2}" class="block positive">{{desc.all}}<span class="count">{{ratings.length}}</span></div>
      <div @click="selectRatingType(0)" :class="{'active':currentSelectType === 0}" class="block positive">{{desc.positive}}<span class="count">{{positives.length}}</span></div>
      <div @click="selectRatingType(1)" :class="{'active':currentSelectType === 1}" class="block negative">{{desc.negative}}<span class="count">{{negatives.length}}</span></div>
    </div>
    <div :class="['switch', 'border-bottom', isOnlyContent?'on':'']">
      <span @click="toggleContent" class="icon icon-check_circle"></span>
      <span class="text">只看有内容的评价</span>
    </div>
  </div>
</template>

<script>

const POSITIVE = 0
const NEGATIVE = 1
const ALL = 2

export default {
  name: 'RatingSelect',
  data() {
    return {
      currentSelectType: this.selectType,
      isOnlyContent: this.onlyContent
    }
  },
  props: {
    ratings:{
      type: Array,
      default() {
        return []
      }
    },
    selectType: {
      type: Number,
      default: ALL
    },
    onlyContent: { // bug onlyContent 不符合预期
      type: Boolean,
      default: false
    },
    desc: {
      type: Object,
      default() {
        return {
          all: '全部',
          positive: '满意',
          negative: '不满意'
        }
      }
    }
  },
  computed: {
    positives() {
      return this.ratings.filter(rating => {
        return rating.rateType === POSITIVE
      })
    },
    negatives() {
      return this.ratings.filter(rating => {
        return rating.rateType === NEGATIVE
      })
    }
  },
  methods: {
    selectRatingType(type) {
      this.currentSelectType = type
      this.$emit("changeCurrentSelectType", this.currentSelectType)
    },
    toggleContent() {
      this.isOnlyContent = !this.isOnlyContent
      this.$emit("changeOnlyContent", this.isOnlyContent)
    }
  },
  // watch: {
  //   selectType(newVal) {
  //     this.selecting = newVal
  //   }
  // }
}
</script>

<style lang="stylus" scoped>
  @import '~@/assets/stylus/mixin'
  .rating-select
    .rating-type
      padding 18px 0
      margin 0 18px
      border-color(rgba(7,17,27,.1))
      font-size 0
      .block
        display inline-block
        margin-right 8px
        padding 8px 12px
        border-radius 1px
        line-height 16px
        font-size 12px
        color rgb(77,85,93)
        &.active
          color #ffffff
        .count
          margin-left 2px
          font-size 8px
        &.positive
          background rgba(0,160,220,.2)
          &.active
            background rgb(0,160,220)
        &.negative
          background rgba(77,85,93,.2)
          &.active
            background rgb(77,85,93)
    .switch
      padding 12px 18px
      line-height 24px
      border-color(rgba(7,17,27,.1))
      color rgb(147,153,159)
      font-size 0
      .icon
        display inline-block
        margin-right 4px
        vertical-align top
        font-size 24px
      &.on
        .icon
          color #00c850
      .text
        font-size 12px
</style>
